import 'package:flutter/material.dart';

typedef OnBarTap =void Function(BarItem barItem);

class AnimatedBottomBar extends StatefulWidget {
  final List<BarItem> barItems ;
  final Duration animationDuration;
  final OnBarTap onBarTap;
  int selectedBarIndex;


  AnimatedBottomBar({@required this.barItems,this.onBarTap ,this.selectedBarIndex = 0,this.animationDuration = const Duration(milliseconds: 200)});

  @override
  _AnimatedBottomBarState createState() => _AnimatedBottomBarState();
}

class _AnimatedBottomBarState extends State<AnimatedBottomBar> with TickerProviderStateMixin{


  @override
  Widget build(BuildContext context) {
    return Material(
      elevation: 10.0,
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 5),
        child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //crossAxisAlignment: CrossAxisAlignment.end,
            children: _buildBarItems(),
          ),

      ),
    );
  }

 List<Widget>  _buildBarItems(){
   List<Widget>_barItems =List();
    for(int i = 0;i< widget.barItems.length;i++){
      BarItem item =  widget.barItems[i];
      var isSelected = widget.selectedBarIndex ==i;
      _barItems.add(InkWell(
        borderRadius: BorderRadius.circular(30),
        highlightColor: Colors.transparent,
        onTap: (){
          setState(() {
            widget.selectedBarIndex = i;
          });
          widget.onBarTap(item);
        },
        child: AnimatedContainer(
          duration: widget.animationDuration,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(30),
              color:isSelected? item.color.withOpacity(0.2):Colors.transparent
          ),
          padding: isSelected?const EdgeInsets.symmetric(vertical:5,horizontal:10):const EdgeInsets.all(5),//left: 20,right: 20,top:8,bottom: 8):const EdgeInsets.all(0),
          child: Row(
              children: <Widget>[
                Icon(item.iconData,color: item.color,),
                SizedBox(width: 5.0,),
                AnimatedSize(
                  duration: widget.animationDuration,
                    vsync: this,
                    curve: Curves.easeInOut,
                    child: Text(isSelected?item.text:'',style: TextStyle(color: item.color,fontWeight: FontWeight.w600),))

              //Text(isSelected?item.text:'',style: TextStyle(color: item.color,fontWeight: FontWeight.w600),)
              ],
            ),
          ),
      ));
    }
    return _barItems;
  }
}

class BarItem{
  String text;
  IconData iconData;
  Color color;
  BarItem({this.text,this.iconData,this.color});
}
/*
List<BarItem> listBarItems = [
  BarItem(text: '首页',iconData: Icons.home,color: Colors.blue[400]),
  BarItem(text: '订单',iconData: Icons.reorder,color: Colors.amberAccent[400]),
  BarItem(text: '购物车',iconData: Icons.shopping_cart,color: Colors.red[400]),
  BarItem(text: '收藏',iconData: Icons.favorite,color: Colors.green[400]),
  BarItem(text: '我的',iconData: Icons.person,color: Colors.black54)
];
*/
